package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.webContent.domain.PlCalculateResult;
import com.ruoyi.webContent.domain.PlMatchType;
import com.ruoyi.webContent.domain.PlTeam;

import java.util.*;

public class PlDataMakeUtils {





    /**
     *
     * @param oddJsonObject
     * @param plMap
     */
    public static Map<String, String> setMatchType(JSONObject oddJsonObject,Map<String, String> ldj_name_id,PlMatchType plMap) {
        String group_short_name = plMap.getLdjMatchName();
        String match_stage = plMap.getLdjOrder();
        String odd = oddJsonObject.get("odds").toString();
        //要返回的值
        Map<String, String> l_match_odds = new HashMap<>();
        if (oddJsonObject.get("group_short_name").equals(group_short_name) && oddJsonObject.get("match_stage").equals(match_stage)
                &&oddJsonObject.getInteger("status")!=4) {//状态等于4的是失效的
            if(plMap.getMatchOrderType().equals("1")){
                l_match_odds.put(ldj_name_id.get(oddJsonObject.getString("team_id")), odd);
            }
            else if(plMap.getMatchOrderType().equals("2")){

            }
            else if(plMap.getMatchOrderType().equals("3")){

            }
            else if(plMap.getMatchOrderType().equals("4")){
                String matchType_name = plMap.getMatchType();
                String value = matchType_name.substring(matchType_name.indexOf("(")+1,matchType_name.indexOf(")"));
                //使用value的绝对值进行匹配
                if(Math.abs(oddJsonObject.getInteger("value"))==Integer.parseInt(value)){
                    l_match_odds.put(oddJsonObject.getString("team_id")+value, odd);
                }
            }
        }
        return l_match_odds;
    }



    /**
     * 从亚博接口的数据格式中获取parentMatch(即需要信息主体)
     *
     * @param y_str
     * @return
     */
    public static JSONArray getYbParentMatch(String y_str) {
        JSONArray y_result = new JSONArray();
        JSONObject y_json = JSONObject.parseObject(y_str);
        Object y_report = null;
        try{
            y_report  = y_json.getJSONArray("Sport").get(0);
        }catch (Exception e){
            e.printStackTrace();
            return y_result;
        }

        JSONArray lg = JSONObject.parseObject(y_report.toString()).getJSONArray("LG");
//        List y_result = new ArrayList();
        for (Object lg_date : lg) {
            JSONObject lg_jsonObject = JSONObject.parseObject(lg_date.toString());
            y_result.addAll(lg_jsonObject.getJSONArray("ParentMatch"));
        }
        return y_result;
    }
/*

    //数据转换成 赔率类型名称 ：（队伍：赔率）
    public static Map<String,Map> yb_contentCase(String yb_code,Map<String,JSONObject> yb_match_contents,Map<String
            , String> yb_name_id,List<PlMatchType> plMatchTypes){
        //获取亚博parentMatch结构
        JSONObject parentMatch = yb_match_contents.get(yb_code);
        Map<String, String> yb_teamMap = new HashMap<>();
        //亚博队伍组装
        yb_teamMap.put("1",yb_name_id.get(parentMatch.getString("PHTName")));
        yb_teamMap.put("2",yb_name_id.get(parentMatch.getString("PATName")));
        //亚博数据处理
        //数据处理完成后的集合 自赔率类型：（队伍：赔率）
        Map<String,Map> host_match_type = new HashMap<>();
        JSONArray MatchArray = parentMatch.getJSONArray("Match");
        for (Object matchObject : MatchArray) {
            JSONObject matchJsonObject = JSONObject.parseObject(matchObject.toString());
            for (PlMatchType plMap : plMatchTypes) {
                Map<String, String> yb_teamid_odd = PlDataMakeUtils.yb_getMatchMap(yb_teamMap, matchJsonObject, plMap);
                if(yb_teamid_odd.size()>0){
                    String yb_matchType_name = "yb_" + plMap.getMatchType();
                    host_match_type.put(yb_matchType_name,yb_teamid_odd);
                    break;
                }

            }

        }
        return host_match_type;
    }
*/


    //通过网站中的队伍名称获取本地队伍id，此处制作map（ldj是id 亚博仍然使用名称）
    public static Map<String,Map<String,String>> getHostTeamLocId(List<PlTeam> plTeams){
        Map<String,String> ldj_name_id_temp = new HashMap<>();
        Map<String,String> lh_name_id_temp = new HashMap<>();
        Map<String,String> yb_name_id_temp = new HashMap<>();
        for (PlTeam plTeam:plTeams) {
            ldj_name_id_temp.put(plTeam.getLdjId(),plTeam.getId().toString());
            lh_name_id_temp.put(plTeam.getTeamLhName(),plTeam.getId().toString());
            yb_name_id_temp.put(plTeam.getTeamSortName(),plTeam.getId().toString());
        }
        Map<String,Map<String,String>>  host_name_id = new HashMap<>();
        host_name_id.put("ldj",ldj_name_id_temp);
        host_name_id.put("lh",lh_name_id_temp);
        host_name_id.put("yb",yb_name_id_temp);
        return host_name_id;
    }
}
